home *** CD-ROM | disk | FTP | other *** search
/ Cre@te Online 2000 December / Cre@teOnline CD05.iso / MacSoft / XML Instance.sea / XML Instance / Required / plugins / HTMLWindow.jar / horst / BusyFlag.class (.txt) < prev    next >
Encoding:
Java Class File  |  2000-03-18  |  2.0 KB  |  58 lines

  1. package horst;
  2.  
  3. public class BusyFlag {
  4.    protected Thread busyflag;
  5.    protected int busycount;
  6.  
  7.    public synchronized void freeBusyFlag() {
  8.       if (this.getBusyFlagOwner() == Thread.currentThread()) {
  9.          --this.busycount;
  10.          if (this.busycount == 0) {
  11.             Utilities.debugOut("freeBusyFlag:" + Thread.currentThread());
  12.             this.busyflag = null;
  13.             String msg = "BusyFlag busy freeing flag " + Thread.currentThread();
  14.             Utilities.debugOut(msg);
  15.             this.notify();
  16.             Utilities.debugOut("After notify");
  17.          }
  18.       }
  19.  
  20.    }
  21.  
  22.    public synchronized void getBusyFlag() {
  23.       while(!this.tryGetBusyFlag()) {
  24.          try {
  25.             Utilities.debugOut("Thread waiting:" + Thread.currentThread());
  26.             this.wait();
  27.             Utilities.debugOut("after waiting:" + Thread.currentThread());
  28.             Utilities.debugOut("after wait");
  29.          } catch (Exception e) {
  30.             System.out.println("Thread:" + Thread.currentThread());
  31.             System.out.println("getBusyFlag exception:" + ((Throwable)e).getMessage());
  32.             System.out.println("busyflag:" + this.busyflag);
  33.             System.out.println("tryGetBusyFlag()=" + this.tryGetBusyFlag());
  34.          }
  35.       }
  36.  
  37.    }
  38.  
  39.    public synchronized Thread getBusyFlagOwner() {
  40.       return this.busyflag;
  41.    }
  42.  
  43.    public synchronized boolean tryGetBusyFlag() {
  44.       if (this.busyflag == null) {
  45.          this.busyflag = Thread.currentThread();
  46.          this.busycount = 1;
  47.          Utilities.debugOut("tryGetBusyFlag:" + Thread.currentThread());
  48.          return true;
  49.       } else if (this.busyflag == Thread.currentThread()) {
  50.          ++this.busycount;
  51.          Utilities.debugOut("tryGetBusyFlag:" + Thread.currentThread());
  52.          return true;
  53.       } else {
  54.          return false;
  55.       }
  56.    }
  57. }
  58.